import en from './en.json'

let messages = {
    en: en,
}
let lang = 'zh_cn';
try { lang = navigator.language; } catch (e) {}
lang = lang.toLowerCase().replace('-','_');

export const LANG = lang
export const LOCALE = lang;

function t(key, replace, locale){
    locale = locale || LOCALE;
    let map = messages[locale] || {};
    let msg = map[key] || key;
    if (replace && typeof replace === 'object'){
        Object.keys(replace).forEach(k=>{
            msg = msg.replaceAll(`:${k}`, replace[k]);
        })
    }
    return msg;
}
// 同时挂载到全局对象
if (typeof window !== 'undefined') {
    window['global_t'] = t
}

export default {
    t: t,
    install(vue) {
        // Vue 3+ 安装方式
        if (vue.config && vue.config.globalProperties){
            vue.config.globalProperties.$t = t;
        }
        // Vue 2 安装方式
        else if (vue.prototype) {
            vue.prototype.$t = t;
        }
    }
};
